

-- Define the ERROR and DEBUG functions if they don't exist.
-- These will be overwritte by the function definitions in the EA_UiDebugTools module.

if( DEBUG == nil ) 
then
    function DEBUG( text )
        LogLuaMessage( "Lua", SystemData.UiLogFilters.DEBUG, text )
    end
end

if( ERROR == nil ) 
then

    function ERROR( text )
        if( DevData.useDevErrorHandling == true ) then
            local strTxt = WStringToString( text )
            error(strTxt, 3)
        else    
            LogLuaMessage( "Lua", SystemData.UiLogFilters.ERROR, text )
        end
    end
end

----------------------------------------------------------------
-- Global Functions
----------------------------------------------------------------

function GetPregameString( id )
	if( id == nil ) then
		ERROR(L"Invalid params to GetPregameString( id): id is nil")
		return L""
	end
	
	return GetStringFromTable( "Pregame", id )
end

function GetPregameStringFormat( id, paramTable)

	if( id == nil ) then
		ERROR(L"Invalid params to GetPregameStringFormat( id, paramTable): id is nil")
		return L""
	elseif( paramTable == nil) then
		ERROR(L"Invalid params to GetPregameStringFormat( id, paramTable): paramTable is nil")
		return L""
	end

    -- If the C-substitution is enabled, use it.
    if(  GetStringFormatFromTable ~= nil ) then
    
		-- Convert all params to wstrings   
		local params = {}        
        local index = 1
        while( paramTable[index] ~= nil ) do
        	params[index] = L""..paramTable[index]
        	index = index + 1
        end
    
         text = GetStringFormatFromTable( "Pregame", id, params )
	
	-- Else, Use the Lua subsitution
	else    

	     text = GetStringFromTable("Pregame", id )
	
	    --DEBUG(L"GetStringFormat: Start Text = "..text )

	    -- Replace each param tag with the variable
	    local paramIndex = 1
	    while paramTable[paramIndex] ~= nil do		
    	
		    local tag = L"<<"..paramIndex..L">>"	
		    local paramText = L""..paramTable[paramIndex]
    		
		    --DEBUG(L"GetStringFormat: Text = "..text..L", Tag = "..tag..L", Param = "..paramText )
    		
		    text = wstring.gsub( text, tag, paramText )
    		
		    paramIndex = paramIndex + 1
	    end
	
	    --DEBUG(L"GetStringFormat: End Text = "..text )
	end	
	
	return text
end


function PregameWStringsCompare( string1, string2 )

    if( string1 == nil ) then ERROR(L"Invalid params to WStringsCompare( string1, string2 ): string1 is nil") end
    if( string2 == nil ) then ERROR(L"Invalid params to WStringsCompare( string1, string2 ): string2 is nil") end

    -- Equals is built in
    if( string1 == string2 ) then
        return 0
    end
    
    -- Comparison is not.
    local len1 = wstring.len(string1)
    local len2 = wstring.len(string2)
    
    for index = 1, len1 do
        if( index > len2) then
            return 1
        end
        
        local char1 = wstring.byte( string1, index )
        local char2 = wstring.byte( string2, index )

        if( char1 < char2 ) then
            return -1
        elseif( char1 > char2 ) then
            return 1
        end     
    end
    
    return -1
end


function PregameStringsCompare( string1, string2 )

    if( string1 == nil ) then ERROR(L"Invalid params to WStringsCompare( string1, string2 ): string1 is nil") end
    if( string2 == nil ) then ERROR(L"Invalid params to WStringsCompare( string1, string2 ): string2 is nil") end

    -- Equals is built in
    if( string1 == string2 ) then
        return 0
    end
    
    -- Comparison is not.
    local len1 = string.len(string1)
    local len2 = string.len(string2)
    
    for index = 1, len1 do
        if( index > len2) then
            return 1
        end
        
        local char1 = string.byte( string1, index )
        local char2 = string.byte( string2, index )

        if( char1 < char2 ) then
            return -1
        elseif( char1 > char2 ) then
            return 1
        end     
    end
    
    return -1
end

PregameDataUtils = {}

--  Alternating Row Colors
PregameDataUtils.RowColors = {}
PregameDataUtils.RowColors[0] = {r=12, g=15, b=22} -- Dark Blue
PregameDataUtils.RowColors[1] = {r=21, g=28, b=35} -- Steel Blue
PregameDataUtils.RowColorHighlighted = {r=16, g=21, b=27} -- Dark Steel Blue



function PregameDataUtils.GetAlternatingRowColor( row_mod_by_two )
    -- Pass the math.mod result of the row number divided by 2
    if( PregameDataUtils.RowColors[row_mod_by_two] ~= nil ) then
        return PregameDataUtils.RowColors[row_mod_by_two]
    end
    return PregameDataUtils.RowColors[0]
end


function PregameDataUtils.AlphabetizeByNames( table1, table2 )

    if( table2 == nil ) then
        return false
    end
    
    if( table1 == nil ) then
        return false
    end
    
    if( table1.name == nil or table2.name == nil ) then
        ERROR(L"PregameDataUtils.AlphabetizeByNames may only be used on tables containing 'name' variables." )
        return false
    end
        
    --DEBUG(L" Name1: "..table1.name..L", Name2: "..table2.name )
    
    -- Sort unknown names (name == L"???" ) to
    -- the end of the list.
    if( table1.name == L"???" ) then
        return false
    end
    
    if( table2.name == L"???" ) then
        return true
    end
    
    if( type( table1.name ) ==  "wstring") then
        return ( PregameWStringsCompare(table1.name, table2.name) < 0 )
    elseif( type( table1.name ) ==  "string") then
        return ( PregameStringsCompare(table1.name, table2.name) < 0 )
    end
    
    return false
end